package 排序算法.希尔排序;

import java.util.Arrays;

/**
 * 希尔排序
 * 时间复杂度：O(n^1.3)
 * 空间复杂度：O(1)
 * 不稳定
 */
public class ShellSort {
    public static void main(String[] args) {
        int[] arr= {8,9,58,5,6,3,5,87,0,5,5};
        shellSort(arr);

        System.out.println(Arrays.toString(arr));
    }

    public static void shellSort(int[] arr) {
        //grap为间距
        for (int grap = arr.length / 2; grap > 0 ; grap = grap / 2) {
            for (int i = grap; i < arr.length; i++) {
                int e = arr[i];
                int j = 0;
                for (j = i; j - grap >= 0 && arr[j - grap] > e ; j = j-grap) {
                    arr[j] = arr[j - grap];
                }
                arr[j] = e;
            }
        }
    }
}
